package lia.analysis;

import java.io.IOException;
import java.io.Reader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.util.Version;

import static lia.util.Constants.*;

/**
 * simple analyzer: from chapter 4
 * 
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2011/08/31
 * @since 1.0
 */
public class SimpleAnalyzer extends Analyzer{
	
	//local variables
	/**
	 * Use by certain classes to match version compatibility across releases of Lucene
	 */
	private final Version matchVersion;
	
	/**
	 * Creates a new {@link SimpleAnalyzer}
	 */
	public SimpleAnalyzer() {
		super();
		this.matchVersion = LUCENE_VERSION;
	}
	
	/**
	 * Creates a new {@link SimpleAnalyzer}
	 * @param matchVersion Lucene version to match See {@link <a href="#version">above</a>}
	 */
	public SimpleAnalyzer(Version matchVersion) {
		super();
		this.matchVersion = matchVersion;
	}



	/**
	 * Creates a TokenStream which tokenizes all the text in the provided
	 * Reader.  Must be able to handle null field name for
	 * backward compatibility.
	 * 
	 * @param fieldName
	 * @param reader
	 */
	@Override
	public TokenStream tokenStream(String fieldName, Reader reader) {
		return new LowerCaseTokenizer(matchVersion, reader);
	}

	/**
	 * Creates a TokenStream that is allowed to be re-used from the previous time that the same thread called
	 * this method.  Callers that do not need to use more than one TokenStream at the same time from this
	 * analyzer should use this method for better performance.
	 *  
	 * @param fieldName
	 * @param reader
	 * @see org.apache.lucene.analysis.Analyzer#reusableTokenStream(java.lang.String, java.io.Reader)
	 */
	@Override
	public TokenStream reusableTokenStream(String fieldName, Reader reader)
			throws IOException {
		Tokenizer tokenizer = (Tokenizer)getPreviousTokenStream();
	    if (tokenizer == null) {
	      tokenizer = new LowerCaseTokenizer(matchVersion, reader);
	      setPreviousTokenStream(tokenizer);
	    }else{
	    	tokenizer.reset(reader);
	    }
	    return tokenizer;
	}
	
	public static void main(String[] args){
		QueryParser qp = new QueryParser(Version.LUCENE_31, "f", new SimpleAnalyzer());
		try {
			System.out.println(qp.parse("test-aa yy"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
//		System.out.println((int)'Z');
//		System.out.println((int)'z');
//		System.out.println((int)'a');
//		System.out.println((int)'A');
//		System.out.println((int)'-');
//		System.out.println((int)'・');
		System.out.println((char)91);
		System.out.println((char)92);
		System.out.println((char)93);
		System.out.println((char)94);
		System.out.println((char)95);
		System.out.println((char)96);
	}
}
